﻿using System;
using System.Xml;

   public class xml
    {
       string XmlFile = string.Empty;
       string XmlNode = string.Empty;
       XmlDocument _xml = new XmlDocument();

       public xml(string xmlfile, string xmlnode)
       {
           XmlFile = xmlfile;
           XmlNode = xmlnode;
           _xml=GetXml(xmlfile);
       }
       
        /// <summary>
        /// 加载XML
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        private static  XmlDocument LoadXml(string FileName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath(FileName));
            }
            catch
            {

            }
            return xmlDoc;
        }
        /// <summary>
        /// 获得XML对象
        /// </summary>
        /// <returns></returns>
        public static XmlDocument GetXml(string xmlfile)
        {
            XmlDocument xml = new XmlDocument();
            try
            {
                xml = LoadXml(xmlfile);   
            }
            catch
            {

            }
            return xml;
        }

        /// <summary>
        /// 读取XML节点信息
        /// </summary>
        /// <returns></returns>
       public XmlNodeList GetInfo()
       {
           XmlNodeList xnl = null;
           try
           {
               if (_xml != null)
               {
                   XmlNode xn = _xml.SelectSingleNode(XmlNode);
                   xnl = xn.ChildNodes;
               }
           }
           catch
           {

           }
           return xnl;

       }

        /// <summary>
        /// 读取XML节点信息
        /// </summary>
        /// <returns></returns>
       public XmlNodeList GetInfo(int page, int pagesize,out int count)
        {

            count = 0;
            XmlNodeList xnl = GetInfo();
            XmlDocument doc = new XmlDocument();
            XmlNode xn = doc.CreateElement("UserInfo");
            
            if (xnl != null && xnl.Count > 0)
            {
                count = xnl.Count;
                for (int i = (page - 1) * pagesize; i < page * pagesize && i < xnl.Count; i++)
                {
                    xn.AppendChild(doc.ImportNode(xnl[i], true));
                }
            }

            return xn.ChildNodes;
        }

       
 
        /// <summary>
        /// 保存XML
        /// </summary>
        /// <returns></returns>
        public  int SaveXml(XmlDocument xml)
        {
            int ret = 0;
            try
            {
                xml.Save(XmlFile);
                ret = 1;
            }
            catch
            {
            
            }
            return ret;
        }


    }

